package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.mapper.NtIeTypeItemMapper;
import com.csun.cmny.provider.mapper.NtIeTypeMapper;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.service.NtIeTypeService;
import com.csun.cmny.provider.service.OmcRpcService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-10 15:17
 * @Description:
 */
@Service
@Transactional
public class NtIeTypeServiceImpl extends BaseService<NtIeType> implements NtIeTypeService {

    @Resource
    private NtIeTypeMapper ntIeTypeMapper;
    @Resource
    private NtIeTypeItemMapper ntIeTypeItemMapper;
    @Resource
    private OmcRpcService omcRpcService;

    /**
     * 费用模块同步
     *
     * @param homeId
     * @param groupId
     * @return
     */
    @Override
    public Integer ieTypeInit(Integer homeId, Long groupId) {

        Integer result = 0;

        // 查询所有的配置项
        List<NtIeType> ntIeTypeList = ntIeTypeMapper.selectAll();
        for (NtIeType type : ntIeTypeList) {
            // TODO 生成新的收费项类别
            OmcExpenseType omcExpenseType = new OmcExpenseType();
            OmcExpenseTypeModelCode omcExpenseTypeModelCode = new OmcExpenseTypeModelCode();

            /*** omcExpenseType 属性 ***/
            String typeName = "";
            Integer code = 0;   // 0-常规类别 1-床位费 2-护理费
            Integer isDelete = 0;   // 0-能删除 大于0-不可以删除(下面有项目就不能删除）
            Integer canMultipleChoose = 0;  // 是否可多选：0-不行，1-可以
            Integer needSelectOne = 0;  // 是否必选一个：0-不是，1-是
            Integer isSelectFirstone = 0;   // 是否默认选中第一个：0-否，1-是
            String omcExpenseTypeRemark = "";   // 备注

            /*** omcExpenseTypeModelCode 属性 ***/
            Integer modelCode = 0;  // 配置码 :1- 入院收费 2-周期收费 3-出院收费 4-非周期收费
            Integer isOutReturn = 0;    // 是否退院退费 0.否 1.是
            Integer isLeaveReturn = 0;  // 是否请假退费 0.否 1.是
            Integer isAdjust = 0;       // 是否可以调整 0.否 1.是

            /*** omcExpenseItem 属性 ***/
            String itemName = "";   // 费用项名称
            BigDecimal chargeStandard = BigDecimal.ZERO;  // 收费标准
            String content = "";    // 服务内容
            String omcExpenseItemRemark = "";   // 备注
            Integer costType = 0;   //  收费频次方式（1-元/次 2-元/天  3-元/月  4-元/年 5-元/一次性）
            Integer itemIsDelete = 0;   // 0-能删除 大于0-不可以删除
            Integer isFixedPrice = 1;   // 是否是固定金额：0-否（不需要填写收费标准），1-是（需要填写收费标准）
            Integer roomDirection = 0;  // 房间类型（1 单人间， 2 双人间， 3 三人间， 4 四人及四人以上）
            Integer hasToilet = 0;  // 是否有厕所 0-否 1-是
            Integer careType = null;   // 护理类型 ：0-自理 1-半失能 3-失能
            Integer roomType = 0;   // 房间类型（1 单人间， 2 双人间， 3 三人间， 4 四人及四人以上）

            switch (type.getIetypeid()) {
                case 1 :   // 一次性费用
                    needSelectOne = 1;
                    isSelectFirstone = 1;
                    omcExpenseTypeRemark = "一次性费用";

                    modelCode = 1;

                    costType = 5;
                    break;
                case 2 :    // 餐饮费
                    needSelectOne = 1;
                    isSelectFirstone = 1;
                    omcExpenseTypeRemark = "餐饮费";

                    modelCode = 2;
                    isOutReturn = 1;
                    isAdjust = 1;
                    isLeaveReturn = 1;

                    costType = 3;
                    break;
                case 3 :    // 公共服务费
                    needSelectOne = 1;
                    isSelectFirstone = 1;
                    omcExpenseTypeRemark = "公共服务费";

                    modelCode = 2;
                    isOutReturn = 1;

                    costType = 3;
                    break;
                case 4 :    // 床位费
                    code = 1;
                    modelCode = 2;
                    omcExpenseTypeRemark = "床位费";

                    costType = 3;
                    break;
                case 5 :    // 护理费
                    code = 2;
                    omcExpenseTypeRemark = "护理费";

                    modelCode = 2;
                    isOutReturn = 1;
                    isAdjust = 1;
                    isLeaveReturn = 1;

                    costType = 3;
                    break;
                case 6 :    // 单项定制服务
                    canMultipleChoose = 1;
                    omcExpenseTypeRemark = "单项定制服务";

                    modelCode = 2;
                    isOutReturn = 1;
                    isAdjust = 1;
                    isLeaveReturn = 1;

                    costType = 3;
                    break;
                case 7 :    // 入住押金
                    code = 3;
                    omcExpenseTypeRemark = "入住押金";

                    modelCode = 1;
                    isOutReturn = 1;

                    costType = 5;
                    break;
            }

            /*** omcExpenseType 属性装配 ***/
            omcExpenseType.setGroupId(groupId);
            omcExpenseType.setCreator("admin");
            omcExpenseType.setCreatorId(1l);
            omcExpenseType.setCreatedTime(type.getCreatetime());
            omcExpenseType.setUpdateTime(type.getModifytime());
            omcExpenseType.setLastOperator("admin");
            omcExpenseType.setLastOperatorId(1l);
            omcExpenseType.setCanMultipleChoose(canMultipleChoose);
            omcExpenseType.setCode(code);
            omcExpenseType.setIsDelete(isDelete);
            omcExpenseType.setIsSelectFirstone(isSelectFirstone);
            omcExpenseType.setNeedSelectOne(needSelectOne);
            omcExpenseType.setRemark(omcExpenseTypeRemark);
            omcExpenseType.setName(omcExpenseTypeRemark);
            omcExpenseType.setVersion(type.getIetypeid());
            Long omcExpenseTypeId = omcRpcService.saveOmcExpenseType(omcExpenseType);

            /*** omcExpenseTypeModelCode 属性装配 ***/
            omcExpenseTypeModelCode.setExpenseTypeId(omcExpenseTypeId);
            omcExpenseTypeModelCode.setGroupId(groupId);
            omcExpenseTypeModelCode.setCreator("admin");
            omcExpenseTypeModelCode.setCreatorId(1l);
            omcExpenseTypeModelCode.setCreatedTime(type.getCreatetime());
            omcExpenseTypeModelCode.setUpdateTime(type.getModifytime());
            omcExpenseTypeModelCode.setLastOperator("admin");
            omcExpenseTypeModelCode.setLastOperatorId(1l);
            omcExpenseTypeModelCode.setIsAdjust(isAdjust);
            omcExpenseTypeModelCode.setIsLeaveReturn(isLeaveReturn);
            omcExpenseTypeModelCode.setIsOutReturn(isOutReturn);
            omcExpenseTypeModelCode.setModelCode(modelCode);
            omcRpcService.saveOmcExpenseTypeModelCode(omcExpenseTypeModelCode);

            NtIeTypeItem record = new NtIeTypeItem();
            record.setHomeid(homeId);
            record.setIetypeid(type.getIetypeid());
            List<NtIeTypeItem> ntIeTypeItemList = ntIeTypeItemMapper.select(record);
            for (NtIeTypeItem item : ntIeTypeItemList) {
                // TODO 生成新收费项
                OmcExpenseItem omcExpenseItem = new OmcExpenseItem();

                if (type.getIetypeid().equals(5)) {
                    careType = item.getBedtype();
                }

                if (type.getIetypeid().equals(4)) {
                    if (item.getBedtype().compareTo(4) > 0) {
                        hasToilet = 1;
                    }
                    roomType = item.getBedtype() - hasToilet * 4;
                }

                omcExpenseItem.setCareType(careType);
                omcExpenseItem.setChargeStandard(item.getChargestandard());
                omcExpenseItem.setExpenseTypeId(omcExpenseTypeId);
                omcExpenseItem.setExpenseTypeName(omcExpenseTypeRemark);
                omcExpenseItem.setContent(item.getContent());
                omcExpenseItem.setCostType(costType);
                omcExpenseItem.setGroupId(groupId);
                omcExpenseItem.setHasToilet(hasToilet);
                omcExpenseItem.setIsFixedPrice(isFixedPrice);
                omcExpenseItem.setItemName(item.getItemname());
                omcExpenseItem.setRemark(item.getItemname());
                omcExpenseItem.setRoomDirection(roomDirection);
                omcExpenseItem.setRoomType(roomType);
                omcExpenseItem.setCreatedTime(item.getCreatetime());
                omcExpenseItem.setUpdateTime(item.getModifytime());
                omcExpenseItem.setCreator("admin");
                omcExpenseItem.setCreatorId(1l);
                omcExpenseItem.setLastOperator("admin");
                omcExpenseItem.setLastOperatorId(1l);
                omcExpenseItem.setVersion(item.getItemid());

                omcRpcService.saveOmcExpenseItem(omcExpenseItem);

                // TODO 映射关系 暂时将老的id存放在version中
            }
        }

        // 物品赔偿 & 出院电费 & 周期电费
        // saveOtherExpense("物品赔偿", modeCode, isOutReturn, isLeveReturn, isAdjust, can, need, select, groupId);
        saveOtherExpense("物品赔偿", 3, 0, 0, 0, 0, 0, 0, groupId);
        saveOtherExpense("出院电费", 3, 0, 0, 0, 0, 0, 0, groupId);
        saveOtherExpense("周期电费", 2, 0, 0, 0, 1, 1, 1, groupId);

        return result;
    }

    public void saveOtherExpense(String name, Integer modeCode, Integer isOutReturn,
                                 Integer isLeaveReturn, Integer isAdjust, Integer can,
                                 Integer need, Integer select, Long groupId) {

        Date now = new Date();
        OmcExpenseType omcExpenseType = new OmcExpenseType();
        omcExpenseType.setGroupId(groupId);
        omcExpenseType.setCreator("admin");
        omcExpenseType.setCreatorId(1l);
        omcExpenseType.setCreatedTime(now);
        omcExpenseType.setUpdateTime(now);
        omcExpenseType.setLastOperator("admin");
        omcExpenseType.setLastOperatorId(1l);
        omcExpenseType.setCanMultipleChoose(can);
        omcExpenseType.setCode(0);
        omcExpenseType.setIsDelete(0);
        omcExpenseType.setIsSelectFirstone(select);
        omcExpenseType.setNeedSelectOne(need);
        omcExpenseType.setRemark(name);
        omcExpenseType.setName(name);
        Long omcExpenseTypeId = omcRpcService.saveOmcExpenseType(omcExpenseType);

        OmcExpenseTypeModelCode omcExpenseTypeModelCode = new OmcExpenseTypeModelCode();
        omcExpenseTypeModelCode.setExpenseTypeId(omcExpenseTypeId);
        omcExpenseTypeModelCode.setGroupId(groupId);
        omcExpenseTypeModelCode.setCreator("admin");
        omcExpenseTypeModelCode.setCreatorId(1l);
        omcExpenseTypeModelCode.setCreatedTime(now);
        omcExpenseTypeModelCode.setUpdateTime(now);
        omcExpenseTypeModelCode.setLastOperator("admin");
        omcExpenseTypeModelCode.setLastOperatorId(1l);
        omcExpenseTypeModelCode.setIsAdjust(isAdjust);
        omcExpenseTypeModelCode.setIsLeaveReturn(isLeaveReturn);
        omcExpenseTypeModelCode.setIsOutReturn(isOutReturn);
        omcExpenseTypeModelCode.setModelCode(modeCode);
        omcRpcService.saveOmcExpenseTypeModelCode(omcExpenseTypeModelCode);

        OmcExpenseItem omcExpenseItem = new OmcExpenseItem();
        omcExpenseItem.setCareType(0);
        omcExpenseItem.setChargeStandard(BigDecimal.ZERO);
        omcExpenseItem.setExpenseTypeId(omcExpenseTypeId);
        omcExpenseItem.setExpenseTypeName(name);
        omcExpenseItem.setContent(name);
        omcExpenseItem.setCostType(5);
        omcExpenseItem.setGroupId(groupId);
        omcExpenseItem.setHasToilet(0);
        omcExpenseItem.setIsFixedPrice(0);
        omcExpenseItem.setItemName(name);
        omcExpenseItem.setRemark(name);
        omcExpenseItem.setRoomDirection(0);
        omcExpenseItem.setRoomType(0);
        omcExpenseItem.setCreatedTime(now);
        omcExpenseItem.setUpdateTime(now);
        omcExpenseItem.setCreator("admin");
        omcExpenseItem.setCreatorId(1l);
        omcExpenseItem.setLastOperator("admin");
        omcExpenseItem.setLastOperatorId(1l);

        omcRpcService.saveOmcExpenseItem(omcExpenseItem);

    }
}
